package com.example.leero.graduation.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class RegisterActivity extends AppCompatActivity {

    private ImageButton buttonBack;
    private TextView title;
    private EditText phone, password, confirmPassword;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        title.setText("注册");
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInput()) {
                    register();
                }
            }
        });
    }

    private void initView() {
        title = (TextView) findViewById(R.id.tv_title);

        phone = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);
        confirmPassword = (EditText) findViewById(R.id.confirm_password);

        buttonBack = (ImageButton) findViewById(R.id.iv_left);
        btnRegister = (Button) findViewById(R.id.btn_register);
    }

    private void register() {
        OkHttpUtils.post().url(API.REGISTER).addParams("phone", phone.getText().toString())
                .addParams("password", password.getText().toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (!jsonObject.getBoolean("state")) {
                        Toast.makeText(RegisterActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                    } else {
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private Boolean checkInput() {
        if (phone.getText().toString().length() != 11) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return false;
        } else if (password.getText().toString().length() < 8) {
            Toast.makeText(this, "密码不能少于8位", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!StringUtil.equals(password.getText().toString(), confirmPassword.getText().toString())) {
            Toast.makeText(this, "两次密码输入不一样", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
